<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 数组去重和排序（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个乱序的数组&#xff0c;删除所有的重复元素&#xff0c;使得每个元素只出现一次&#xff0c;并且按照出现的次数从高到低进行排序&#xff0c;相同出现次数按照第一次出现顺序进行先后排序。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>一个数组</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>去重排序后的数组</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td>输入</td><td>1,3,3,3,2,4,4,4,5</td></tr><tr><td>输出</td><td>3,4,1,2,5</td></tr><tr><td>备注</td><td>数组大小不超过100 数组元素值大小不超过100。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>简单的排序问题。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34;,&#34;);
  console.log(getResult(arr));
});

/* 算法逻辑 */
function getResult(arr) {
  const count &#61; {};
  const first &#61; {};

  for (let i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
    const s &#61; arr[i];

    if (count[s] &#61;&#61; undefined) count[s] &#61; 0;
    count[s]&#43;&#43;;

    if (first[s] &#61;&#61; undefined) first[s] &#61; i;
  }

  return [...Object.keys(first)]
    .sort((a, b) &#61;&gt;
      count[a] !&#61; count[b] ? count[b] - count[a] : first[a] - first[b]
    )
    .join(&#34;,&#34;);
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.HashMap;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String[] arr &#61; sc.nextLine().split(&#34;,&#34;);
    System.out.println(getResult(arr));
  }

  public static String getResult(String[] arr) {
    HashMap&lt;String, Integer&gt; count &#61; new HashMap&lt;&gt;();
    HashMap&lt;String, Integer&gt; first &#61; new HashMap&lt;&gt;();

    for (int i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
      String s &#61; arr[i];
      count.put(s, count.getOrDefault(s, 0) &#43; 1);
      first.putIfAbsent(s, i);
    }

    StringJoiner sj &#61; new StringJoiner(&#34;,&#34;);

    first.keySet().stream()
        .sorted(
            (a, b) -&gt; {
              int countA &#61; count.get(a);
              int countB &#61; count.get(b);

              if (countA !&#61; countB) {
                return countB - countA;
              } else {
                int firstA &#61; first.get(a);
                int firstB &#61; first.get(b);
                return firstA - firstB;
              }
            })
        .forEach(s -&gt; sj.add(s));

    return sj.toString();
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; input().split(&#34;,&#34;)


# 算法入口
def getResult(arr):
    count &#61; {}
    first &#61; {}

    for i in range(len(arr)):
        s &#61; arr[i]

        if count.get(s) is None:
            count[s] &#61; 0
        count[s] &#43;&#61; 1

        if first.get(s) is None:
            first[s] &#61; i

    tmp &#61; list(first.keys())
    tmp.sort(key&#61;lambda x: (-count[x], first[x]))
    return &#34;,&#34;.join(tmp)


# 算法调用
print(getResult(arr))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>